package com.common.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.common.util.StringToken;

public class LoginAction extends AbstractCommonAction {
	private static final long serialVersionUID = 1L;
	Log logger = LogFactory.getLog(this.getClass());  
	private String loginName;

	private String loginPassword;

	// 显示登陆界面
	@SuppressWarnings("unchecked")
	@Override
	@SkipValidation
	public String execute() throws Exception {
		this.getSession().put(StringToken.CURRENT_LOGIN, null);
		return SUCCESS;
	}

	// 处理登陆
	@SuppressWarnings("unchecked")
	public String doLogin() throws Exception {
		Object obj = null;
		// 登陆
		logger.info("user start login !");
		obj = commonService.findUser(loginName, loginPassword);
		if (obj == null) {
			this.addActionMessage("Message:没有此用户！");
			this.addActionError("Error:没有此用户！");
			return INPUT;
		}
		logger.info("user login successful !" + loginPassword);
		// 保存登陆用户的信息
		this.getSession().put(StringToken.CURRENT_LOGIN, obj);
		return SUCCESS;
	}

	// 退出登陆
	@SuppressWarnings("unchecked")
	@SkipValidation
	public String logout() {
		this.getSession().put(StringToken.CURRENT_LOGIN, null);
		logger.info("User logout successful !");
		return LOGIN;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

}
